package cate.game.activity.weekcard;

import cate.common.table.activity.weekcard.row.WeekCardRow;
import cate.common.table.d.GDActivity;
import cate.common.table.d.GDOperation;
import cate.common.table.pay.PayItemRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.weekcard.msg.WeekCardActivityInfoResp;
import cate.game.activity.weekcard.po.WeekCardItemPo;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class WeekCardActivity extends RoleActivitySingle {

    @NoteField("周卡map")
    public Map<Integer, WeekCardItemPo> cardMap;

    @NoteField("活动当前天数")
    public int today;

    public static final int MERGE_DAY = 8;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (cardMap == null) {
            cardMap = new HashMap<>();
        }
        cardMap.values().forEach(item -> item.initialize(role));
    }

    public void noticeUpdate() {
        role.sendNow(new WeekCardActivityInfoResp(this));
    }

    @Override
    public void excelCheck() {
        Map<Integer, WeekCardRow> allTasks = getConfigs();
        if (allTasks.isEmpty()) {
            cardMap.clear();
            return;
        }
        cardMap.entrySet().removeIf(kv -> !allTasks.containsKey(kv.getKey()));
        for (WeekCardRow card : allTasks.values()) {
            if (!cardMap.containsKey(card.id)) {
                WeekCardItemPo po = new WeekCardItemPo(card.id, false);
                po.initialize(role);
                cardMap.put(card.id, po);
            }
        }
    }

    @JsonIgnore
    private Map<Integer, WeekCardRow> getConfigs() {
        Map<Integer, WeekCardRow> retMap = new HashMap<>();
        retMap.putAll(role.getGame().table.weekCard.weekCard.getRowsByFuncAndConfigId(funcId(), configTid())
                .stream().collect(Collectors.toMap(e -> e.id, v -> v)));
        return retMap;
    }

    @Override
    public void onRoleOpen() {

    }

    @Override
    public void onRoleEnd() {
        sendUnclaimedDailyReward();
        cardMap.clear();
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    @Override
    public GameResult<Void> allowPay(PayItemRow pir) {
        GameResult<Void> r = new GameResult<>();
        int tid = pir.funcSubId;
        WeekCardRow row = getRow(tid);
        if (row == null) {
            return r.fail("未找到配置");
        }
        if (!cardMap.containsKey(tid)) {
            return r.fail("此周卡不存在");
        }
        if (cardMap.get(tid).buy) {
            return r.fail("请勿重复购买");
        }
        MixRes res = new MixRes(row.reward);
        if (res.isEmpty()) {
            return r.fail("奖励为空");
        }
        if (row.buyType != GDActivity.SHOP_TYPE_PAY) {
            return r.fail("不支持此支付方式");
        }
        return r.success();
    }

    @Override
    public void onPaySuccess(PayItemRow pir) {
        int tid = pir.funcSubId;
        provideReward(tid);
    }

    public void provideReward(int tid) {
        WeekCardRow row = getRow(tid);
        if (row == null) {
            return;
        }
        //发送购买奖励
        MixRes res = new MixRes(row.reward);
        if (res.isEmpty()) {
            return;
        }
        cardMap.get(tid).buy = true;
        res.add(role, true, GDOperation.WEEK_CARD, tid);
        role.getGame().notice.dialogReward(role, res);
        today = getCurrentDay();
        //发送每日奖励
//        sendUnclaimedDailyReward();
        noticeUpdate();
    }

    private void sendUnclaimedDailyReward() {
        //遍历周卡
        today = getCurrentDay();
        cardMap.values().forEach(item -> item.reissue(today));
    }

    private WeekCardRow getRow(int tid) {
        return role.getGame().table.weekCard.weekCard.get(tid);
    }

    @JsonIgnore
    public GameResult<WeekCardActivity> getReward(int tid, int day) {
        GameResult<WeekCardActivity> r = new GameResult<>();
        r.data = this;
        WeekCardItemPo po = cardMap.get(tid);
        if (po == null) {
            return r.fail("周卡不存在");
        }
        if (!po.buy) {
            return r.fail("请购买后领取");
        }
        today = getCurrentDay();
        if (day <= 0) {
            return r.fail("日期不合法");
        }
        if (po.drawList.contains(day)) {
            return r.fail("不能重复领取");
        }
        if (day > today) {
            return r.fail("还没到领取时间");
        }
        MixRes res = po.getReward(day);
        res.add(role, true, GDOperation.WEEK_CARD_DAILY, tid);
        role.getGame().notice.dialogReward(role, res);
//        po.drawList.add(day);
        return r.success();
    }

    public GameResult<WeekCardActivity> buyCard(int tid) {
        GameResult<WeekCardActivity> r = new GameResult<>();
        r.data = this;
        WeekCardRow row = getRow(tid);
        if (row == null) {
            return r.fail("未找到配置");
        }
        if (!cardMap.containsKey(tid)) {
            return r.fail("此周卡不存在");
        }
        if (cardMap.get(tid).buy) {
            return r.fail("请勿重复购买");
        }
        if (row.buyType != GDActivity.SHOP_TYPE_ITEM_COST) {
            return r.fail("不支持此支付方式");
        }
        MixRes consume = new MixRes(row.cost);
        GameResult<MixResItem> c = consume.consume(role);
        if (!c.ok()) {
            return r.fail("道具数量不足");
        }
        provideReward(tid);
        return r.success();
    }

    @Override
    public void onMergeSettle() {
        today = MERGE_DAY;
        cardMap.values().forEach(item -> item.reissue(today));
    }
}
